<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <!-- 与文座在司天测试前后端分离初步2020年8月13日 -->
    <form action="http://192.168.0.125:8080/testuser" method="POST">
      <input type="submit" value="请求" />
    </form>

    <input type="text" class="uname" placeholder="uname" />
    <input type="password" class="pwd" placeholder="pwd" />
    <button>请求</button>
    <div id="app">
      <ul>
        <li v-for="item in list">{{item.id}}--{{item.name}}--{{item.money}}</li>
      </ul>
    </div>
    <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.11/vue.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <!-- https://www.cnblogs.com/luffe/p/7744228.html
        https://blog.csdn.net/weixin_43490452/article/details/107539649
          https://www.cnblogs.com/oukele/p/9464367.html
  -->
    <script>
      const vm = new Vue({
        el: "#app",
        data: {
          list: [],
        },
      });

      /*原生ajax实现*/
      // let ajax = new XMLHttpRequest();
      // ajax.open("GET", "http://192.168.0.125:8080/testuser");
      // ajax.send();
      // ajax.onreadystatechange = function () {
      //   if (ajax.status === 200 && ajax.readyState === 4) {
      //     console.log(JSON.parse(ajax.response));
      //     console.log(ajax.response);
      //   }
      // };

      /*$.ajax实现get/post/data请求*/
      $("button").click(function () {
        let uname = $(".uname").val();
        let pwd = $(".pwd").val();
        let d = {
          username: uname,
          password: pwd,
        };
        $.ajax({
          // url: `http://192.168.0.125:8080/testuser?uname=${uname}&pwd=${pwd}`,
          url: `http://192.168.0.125:8080/testuser`,
          type: "POST",
          contentType: "application/json",
          data: JSON.stringify(d),
          success: function (result) {
            // console.log(result);
          },
          error: function () {
            console.log("Send Request Fail..");
          },
        });
      });

      /*fetch实现get请求*/
      // fetch("http://192.168.0.125:8080/testuser")
      //   .then((response) => response.json())
      //   .then((res) => {
      //     console.log(res);
      //     vm.list = res;
      //   })
      //   .catch((e) => console.log("Oops, error", e));

      /*fetch实现post请求*/
      fetch("http://192.168.0.125:8080/testuser", {
        method: "POST",
        // mode: "cors", //跨域请求
        headers: {
          "Content-Type": "application/json",
        },
        body: JSON.stringify({
          id: "XXX",
        }),
      })
        .then((response) => response.json())
        .then((res) => {
          console.log(res);
          vm.list = res;
        })
        .catch((e) => console.log("Oops, error", e));
    </script>
  </body>
</html>
